*************************************************************************************************************************************;
*This do file estimates the initial conditions for the model with parental education;
*************************************************************************************************************************************;

*Open data;
use $scratch\final_data_for_model.dta, clear


factor cogn1_tilde cogn2_tilde cogn3_tilde cogn4_tilde cogn5_tilde, factors(1)
predict skills_hat if e(sample), bartlett
qui sum skills_hat if class==9
replace skills_hat = (skills_hat-r(mean))/r(sd)


egen mean_skills_school_9 = mean(skills_hat) if class==9, by(idschool)
egen sd_skills_school_9 = sd(skills_hat) if class==9, by(idschool)

gen n_kids = 1 

gen college_mother = (educ_mother>=16) if educ_mother!=.


gen share_college_mother = college_mother

preserve
collapse  mean_skills_school_9 sd_skills_school_9 share_college_mother (count) n_kids if class==9 & school_size>200, by(idschool)

xtile quartiles_schools = mean_skills_school, nquantiles(4)
drop if quartiles_schools==.
drop if n_kids<=10

keep idschool quartiles_schools share_college_mother n_kids
save $scratch\schools_initial_cond.dta, replace

collapse share_college_mother n_kids , by(quartiles_schools)
save $scratch\schools_share_college.dta, replace

restore

drop _merge
merge m:1 idschool using $scratch\schools_initial_cond.dta, keepusing(quartiles_schools)
keep if _merge==3
drop _merge

egen mean_skills_school_9_educ = mean(skills_hat) if class==9, by(idschool college_mother)
egen sd_skills_school_9_educ = sd(skills_hat) if class==9, by(idschool college_mother)
drop if college_mother==.
collapse  mean_skills_school_9_educ sd_skills_school_9_educ  if class==9 & school_size>200, by(quartiles_schools college_mother)

merge m:1 quartiles_schools using $scratch\schools_share_college.dta, keepusing(share_college_mother n_kids)

gen total_kids = round(n_kids)
drop n_kids
drop _merge

sort college_mother quartiles_schools 

drop quartiles_schools college_mother 

order  mean_skills_school_9_educ sd_skills_school_9_educ total_kids   share_college_mother

outsheet using "$matlab_dir\initial_conditions_educ.txt", nonames replace

